<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Helper\Upload;
use Think\Image;
use Common\Controller\BaseController;


class UploadController extends AdminController {

    //上传设置
    public function upload()
    {
        $this->display();
    }
     public function Imgsucai() {
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = 3145728; // 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->rootPath = './'; // 设置附件上传根目录
        $upload->savePath = 'Uploads/'; // 设置附件上传（子）目录
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功
            foreach ($info as $file) {
                $file_path=__ROOT__ . '/' . $file['savepath'] . $file['savename'];
                print_r($file_path);
            }
        }
    }
    //上传图片
    public function logo(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     0 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','pdf','mp4');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            echo json_encode(['status' => -1, 'msg' => $upload->getError()]);exit();
        }else{// 上传成功
            foreach($info as $file){
                $bigimg ='/Uploads/'.$file['savepath'].$file['savename'];
            }
        }
         echo $bigimg;
         exit;
    }

    //上传图片 detail_info
    public function detailInfoLogo()
    {
        include VENDOR_PATH . 'aliyunOss/autoload.php';
        //  方案二 $base_img是获取到前端传递的src里面的值，也就是我们的数据流文件
        $bigimg = [];
        foreach ($_POST['img_arr'] as $base_img)
        {
            $base_img = str_replace('data:image/jpeg;base64,', '', $base_img);
            //  设置文件路径和文件前缀名称
            // $path = "./Uploads/".date('Y-m-d').'/';
            $path = '';
            $realPath = "/Uploads/".date('Y-m-d').'/';
            if (! file_exists ( $path ))
            {
                mkdir ( "$path", 0777, true );
            }
            $prefix      ='ry_';
            $output_file = $prefix.time().rand(100,999).'.jpg';
            $path        = $path.$output_file;
            $realPath    = "/Uploads/".date('Y-m-d').'/'.$output_file;
            //  创建将数据流文件写入我们创建的文件内容中
            $ifp = fopen( $path, "wb" );
            fwrite( $ifp, base64_decode( $base_img) );
            fclose( $ifp );
            // 第二种方式
            // file_put_contents($path, base64_decode($base_img));
            // 存文件到数据库
            $ss = $this->uploadAliyunOssWithBinary($path);
            array_push($bigimg,$ss);

        }

        $where = ['user_id' => $_POST['user_id'],'e_file_key' => $_POST['e_file_key']];
        $checkUserFile = M('user_file')->where($where)->find();
        $strBigimg = implode(',',$bigimg);

        if ($checkUserFile)
        {
            M('user_file')->where($where)->save(['file_dist' => $strBigimg]);
        }else{
            $where['file_dist']   = $strBigimg;
            $where['file_key']    = $_POST['file_key'];
            $where['create_time'] = time();
            M('user_file')->data($where)->add();
        }
        echo json_encode(['status' => 0, 'msg' => $bigimg[0],'info' => $bigimg]);exit();

        /* //之前file形式传输数据 方案一
        $upload           = new \Think\Upload();// 实例化上传类
        $upload->maxSize  = 2097152 ;// 设置附件上传大小
        $upload->exts     = array('jpg', 'gif', 'png', 'jpeg','pdf','mp4');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();
        */

        // $bigimg = [];
        // if(!$info) {// 上传错误提示错误信息
        //     echo json_encode(['status' => -1, 'msg' => $upload->getError()]);exit();
        // }else{// 上传成功
        //     foreach($info as $file){
        //         array_push($bigimg,'/Uploads/'.$file['savepath'].$file['savename']);
        //         // $bigimg ='/Uploads/'.$file['savepath'].$file['savename'];
        //     }
        // }

        // exit;
    }

    //上传视频 detail_info
    public function detailInfoVideo()
    {
        include VENDOR_PATH . 'aliyunOss/autoload.php';
        $bigimg = $this->uploadAliyunOss('file');

        //之前file形式传输数据 方案一
        // $upload           = new \Think\Upload();// 实例化上传类
        // $upload->maxSize  = 0 ;// 设置附件上传大小
        // $upload->exts     = array('mp4');// 设置附件上传类型
        // $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        // $upload->savePath = ''; // 设置附件上传（子）目录
        // // 上传文件
        // $info   =   $upload->upload();

        // $bigimg = [];
        // if(!$info) {// 上传错误提示错误信息
        //     echo json_encode(['status' => -1, 'msg' => $upload->getError()]);exit();
        // }else{// 上传成功
        //     foreach($info as $file){
        //         array_push($bigimg,'/Uploads/'.$file['savepath'].$file['savename']);
        //         // $bigimg ='/Uploads/'.$file['savepath'].$file['savename'];
        //     }
        // }

        $where         = ['user_id' => $_POST['user_id'],'e_file_key' => $_POST['e_file_key']];
        $checkUserFile = M('user_file')->where($where)->find();
        // $strBigimg     = implode(',',$bigimg);

        if ($checkUserFile)
        {
            M('user_file')->where($where)->save(['file_dist' => $bigimg]);
        }else{
            // $where['file_dist']   = $strBigimg;
            $where['file_dist']   = $bigimg;
            $where['file_key']    = $_POST['file_key'];
            $where['create_time'] = time();
            M('user_file')->data($where)->add();
        }

        // echo json_encode(['status' => 0, 'msg' => $bigimg[0],'info' => $bigimg]);
        echo json_encode(['status' => 0, 'msg' => $bigimg,'info' => $bigimg]);
        exit;
    }

}
